{taglib name="peter" /}
<!doctype html>
<html>
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <title>{block name="title"}WeiPeter{/block}</title>
    <link rel="stylesheet" href="__STATIC__/lib/bootstrap/css/bootstrap.min.css">
    <script src="__STATIC__/lib/jquery/jquery-2.1.4.min.js"></script>
    <script src="__STATIC__/lib/bootstrap/js/bootstrap.min.js"></script>
    <script src="__STATIC__/onepeter/js/admin.js"></script>
    <link rel="stylesheet" href="__STATIC__/onepeter/css/admin.css">
    <script>
        /**
         * 上传文件
         */
        $(function () {
            // 上传文件函数
            function uploadFile(fileInput,url,successFunc,errorFunc) {
                var formData = new FormData();
                $(fileInput.files).each(function (index,val) {
                    formData.append('file' + index,val);
                });
                $.ajax({
                    url: url,
                    type: "POST",
                    data: formData,
                    /**
                     *必须false才会自动加上正确的Content-Type
                     */
                    contentType: false,
                    /**
                     * 必须false才会避开jQuery对 formdata 的默认处理
                     * XMLHttpRequest会对 formdata 进行正确的处理
                     */
                    processData: false,
                    success: successFunc,
                    error: errorFunc
                })
            }


            // 当已上传的图片被取消
            $('.file').on('click','.img-close',function () {
                imgValue = $(this).attr("img-value");
                var hidden = $(this).parents('.file').find('.img-hidden');
                var value = hidden.prop('value');
                var valueArr = value.split(',');
                var newValueArr = [];
                for(var key in valueArr) {
                    if(valueArr[key] != imgValue) {
                        newValueArr.push(valueArr[key]);
                    }
                }
                value = newValueArr.join(',');
                hidden.prop('value',value);
            });
            // 当上传图片按钮被点击
            $('.file .img-input').on("change",function () {
                var url = "{:url('admin/Core/uploadImg')}";
                var input = this;
                uploadFile(this,url,function (data) {
                    $(data.ids).each(function (index,val) {
                        var img = '<div class="upload-img alert" style="display: inline-block">\
                            <button type="button" img-value="'+val.id+'" class="img-close close" data-dismiss="alert" aria-label="Close">\
                            <span aria-hidden="true">&times;</span>\
                    </button>\
                    <img  class="img-thumbnail" src="'+val.url+'">\
                    </div>';
                        $(input).parents('.file').append(img);
                        var hidden = $(input).parents('.file').find('.img-hidden');
                        var value = hidden.prop('value');
                        if(value) {
                            var valueArr = value.split(',');
                            valueArr[valueArr.length] = val.id;
                            value = valueArr.join(',');
                            hidden.prop('value',value);
                        }else {
                            hidden.prop('value',val.id);
                        }


                    })
                },function (data) {

                });
            });
            // 当单图片上传按钮被点击
            $('.file .single-img-input').on("change",function () {
                var url = "{:url('admin/Core/uploadImg')}";
                var input = this;
                uploadFile(this,url,function (data) {
                    $(data.ids).each(function (index,val) {
                        var img = $(input).parents('.file').children("div.upload-img");
                        if(img) {
                            img.remove();
                        }


                        img = '<div class="upload-img alert" style="display: inline-block">\
                        <button type="button" img-value="'+val.id+'" class="img-close close" data-dismiss="alert" aria-label="Close">\
                        <span aria-hidden="true">&times;</span>\
                </button>\
                <img  class="img-thumbnail" src="'+val.url+'">\
                </div>';
                        $(input).parents('.file').append(img);


                        var hidden = $(input).parents('.file').find('.img-hidden');
                        var value = hidden.prop('value');
                        hidden.prop('value',val.id);


                    })
                },function (data) {

                });
            });







            // 当上传文件按钮被点击
            $('.file .file-input').on("change",function () {
                var url = "{:url('admin/Core/uploadFile')}";
                var input = this;
                uploadFile(this,url,function (data) {
                    $(data.ids).each(function (index,val) {
                        var img = '<div class="upload-file alert" style="display: inline-block">\
                                <button type="button" file-value="'+val.id+'" class="file-close close" data-dismiss="alert" aria-label="Close">\
                                <span aria-hidden="true">&times;</span>\
                        </button>\
                        <span>'+val.url+'</span>\
                        </div>';


                        $(input).parents('.file').append(img);
                        var hidden = $(input).parents('.file').find('.file-hidden');
                        var value = hidden.prop('value');
                        if(value) {
                            var valueArr = value.split(',');
                            valueArr[valueArr.length] = val.id;
                            value = valueArr.join(',');
                            hidden.prop('value',value);
                        }else {
                            hidden.prop('value',val.id);
                        }


                    })
                },function (data) {

                });
            });

            // 当单文件上传按钮被点击
            $('.file .single-file-input').on("change",function () {
                var url = "{:url('admin/Core/uploadFile')}";
                var input = this;
                uploadFile(this,url,function (data) {
                    $(data.ids).each(function (index,val) {
                        var img = $(input).parents('.file').children("div.upload-file");
                        if(img) {
                            img.remove();
                        }



                        var img = '<div class="upload-file alert" style="display: inline-block">\
                                <button type="button" file-value="'+val.id+'" class="file-close close" data-dismiss="alert" aria-label="Close">\
                                <span aria-hidden="true">&times;</span>\
                        </button>\
                        <span>'+val.url+'</span>\
                        </div>';


                        $(input).parents('.file').append(img);
                        var hidden = $(input).parents('.file').find('.file-hidden');
                        hidden.prop('value',val.id);


                    })
                },function (data) {

                });
            });

            // 当已上传的图片被取消
            $('.file').on('click','.file-close',function () {
                imgValue = $(this).attr("file-value");
                var hidden = $(this).parents('.file').find('.file-hidden');
                var value = hidden.prop('value');
                var valueArr = value.split(',');
                var newValueArr = [];
                for(var key in valueArr) {
                    if(valueArr[key] != imgValue) {
                        newValueArr.push(valueArr[key]);
                    }
                }

                value = newValueArr.join(',');

                hidden.prop('value',value);
            });
        });
    </script>
    {block name="head"}{/block}
    {block name="style"}{/block}
</head>
<body>
{include file="admin@admin_widget:navtoolbar" /}
<div class="container-fluid container-main">
    <div class="row fill-height">
        {include file="admin@admin_widget:navsidebar" /}
        <div class="col-xs-12 col-sm-9 col-md-10 main fill-height">
            <div class="content fill-height">
                {notempty name="_title"}
                <h2>{$_title}</h2>
                {/notempty}
                {notempty name="_suggest"}
                <h3>({$_suggest})</h3>
                {/notempty}
                <hr>
                <div class="main-content">
                    {block name="content"}{/block}
                </div>
            </div>
        </div>
    </div>

</div>

{block name="script"}{/block}
</body>
</html>

















